package cn.gaotu.org.service.impl;

import cn.gaotu.basic.excepion.BusinessException;
import cn.gaotu.basic.util.BaiduAuditUtils;
import cn.gaotu.org.domain.Employee;
import cn.gaotu.org.domain.Shop;
import cn.gaotu.org.domain.ShopAuditLog;
import cn.gaotu.org.vo.ShopVo;
import cn.gaotu.org.mapper.EmployeeMapper;
import cn.gaotu.org.mapper.ShopAuditLogMapper;
import cn.gaotu.org.mapper.ShopMapper;
import cn.gaotu.org.service.ShopService;
import cn.gaotu.basic.service.impl.BaseServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements ShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired
    private JavaMailSender javaMailSender;

    @Override
    @Transactional
    public void settlement(Shop shop) {
        //一：校验 1.空值校验(写一个) 2.格式通过正则表达式（不写） 3.校验是否入驻过
        // 3.确认密码校验（不写） - 前端已经校验了后端还有校验，前端js代码校验可能会被跳过
        //空值校验 - a.直接使用字符串的方法 b.使用apache提供的lang3包 c.使用其他工具类
        if (StringUtils.isEmpty(shop.getName())) {
            throw new BusinessException("店铺名称不能为空");
        }
        //通过name和地址进行获取数据库的值
        Shop dbShop = shopMapper.loadByNameAndAddress(shop);
        if (dbShop != null) {//说明根据名称和地址找到了 - 这个店铺注册过了
            throw new BusinessException("店铺已经入驻，请直接登录!如果忘记密码，请联系管理员!!!");
        }
        if (!BaiduAuditUtils.TextCensor(shop.getName())) {
            throw new BusinessException("不要搞事情,我们都还年轻");
        }
        //二：添加管理员信息 - 添加了之后就可以获取自增ID
        Employee admin = shop.getAdmin();
        employeeMapper.save(admin);

        //三：将自增id设置到t_shop的admin_id
        shop.setAdminId(admin.getId());

        //四：添加Shop
        shopMapper.save(shop);

        //五：将shop_id更新到t_employee
        admin.setShop_id(shop.getId());
        employeeMapper.update(admin);
    }

    /**
     * 不通过
     *
     * @param log
     */
    @Override
    public void reject(ShopAuditLog log) throws MessagingException {
        //1.更新
        Shop shop = shopMapper.findById(log.getShopId());
        shop.setState(4);
        shopMapper.update(shop);
        //2.记录日志
        shopAuditLogMapper.save(log);

        //3.关联店铺管理员
        Employee manager = employeeMapper.findById(shop.getAdminId());
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
        helper.setFrom("798269084@qq.com");
        helper.setSubject("着火了你都不回来");
        //第二个参数 true 代表是否是一个html内容
        helper.setText("<h1>审核不成功,请重新输入填写</h1>"
                , true);
        //添加附件
        helper.addAttachment("压缩文件", new File("C:\\Users\\Zou\\Pictures\\20190306134520829.jpg"));
        //收件人
        helper.setTo(manager.getEmail());
        //发送邮件
        javaMailSender.send(mimeMessage);

        //删除对应数据
        shopMapper.DeleteDep(log.getShopId());
        employeeMapper.DeleteDep(shop.getAdminId());
    }

    @Override
    public void pass(ShopAuditLog log) throws MessagingException {
        //1.更新
        Shop shop = shopMapper.findById(log.getShopId());
        shop.setState(2);
        shopMapper.update(shop);
        //2.记录日志
        shopAuditLogMapper.save(log);
        //3.关联店铺管理员
        Employee manager = employeeMapper.findById(shop.getAdminId());
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
        helper.setFrom("798269084@qq.com");
        helper.setSubject("审核成功请激活邮箱");
        //第二个参数 true 代表是否是一个html内容
        helper.setText("<h1>点击激活</h1>http://localhost:8080/shop/passUpdate/" + shop.getId()
                , true);
        //收件人
        helper.setTo(manager.getEmail());
        //发送邮件
        javaMailSender.send(mimeMessage);

    }

    @Override
    public List<ShopVo> countByState() {
        return shopMapper.countByState();
    }
}
